package com.yenlien.traceyun.plant.dao;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.plant.entity.Company;
import com.yenlien.traceyun.webase.dao.BaseJpaDao;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author jialong
 * @date 2021/4/15 21:16
 * @description:
 **/
public interface CompanyDao extends BaseJpaDao<Company> {

    @Query(value = "SELECT * FROM t_company tc LEFT JOIN dock_zhongyang dz ON tc.id = dz.company_id WHERE dz.company_id IS NULL", nativeQuery = true)
    List<Company> getUnDockComp();

    @Query(value = "SELECT * FROM t_company WHERE id IN(?1)", nativeQuery = true)
    List<Company> getCompanyByIds(List<String> ids);

    @Query(value = "SELECT id, abbreviation, fruit_prod fruitProd FROM t_company WHERE id IN(?1)", nativeQuery = true)
    List<JSONObject> getNameProdByIds(List<String> ids);

    @Query(value = "SELECT * FROM t_company WHERE parent_id = ?1 OR id = ?1", nativeQuery = true)
    List<Company> getByPid(String companyId);

    @Query(value = "SELECT id, IF(abbreviation IS NULL, `name`, abbreviation) name FROM t_company WHERE parent_id = ?1", nativeQuery = true)
    List<JSONObject> getPlantComp(String companyId);

    @Query(value = "SELECT id, `address`, `credit_code` credit_code, `legal_name` username, `legal_phone` mobile, `name` FROM t_company " +
            "WHERE parent_id IN (?1) AND (dock_province = '2' OR dock_province IS NULL)", nativeQuery = true)
//            "WHERE parent_id IN (?1) AND (dock_province = '2' OR dock_province IS NULL) AND (legal_name IS NOT NULL AND legal_name != '')", nativeQuery = true)
    List<JSONObject> dock3(List<String> parentIdList);

    Company findById(String id);

    @Transactional
    @Modifying
    @Query(value = "update t_company set dock_province = '1' where id IN (?1)", nativeQuery = true)
    Integer updateDocked(List<String> idList);


    @Query(value = "SELECT id, credit_code FROM t_company WHERE parent_id IN (?1) AND (dock_province = '1') ", nativeQuery = true)
    List<JSONObject> getIdByPid(List<String> pidList);


    @Query(value = "SELECT id, name, abbreviation, fruit_type fruitType, comp_type compType, address,  operate_name operateName, fruit_prod fruitProd, logo_img logoImg FROM t_company WHERE parent_id = '4028814b89ae9d99018ab0822fd9002d'", nativeQuery = true)
    List<JSONObject> getFruit();

    // 果园企业的所有ID
    @Query(value = "SELECT id FROM t_company WHERE parent_id = '4028814b89ae9d99018ab0822fd9002d'", nativeQuery = true)
    List<String> getFruitId();

}
